package leetcode.median;

/**
 * ClassName: $213_Rob
 * Description:
 * date: 2022-02-20 15:36
 *
 * @author liyifan
 */
public class $213_Rob {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {return 0;}
        int len = nums.length;
        if (len == 1) return nums[0];
        if (len == 2) {return Math.max(nums[0], nums[1]);}
        return Math.max(dp(nums, 0, len - 2), dp(nums, 1, len - 1));
    }

    public int dp(int[] nums, int start, int end){
        // 滚动数组
        int first = nums[start];
        int second = Math.max(nums[start], nums[start + 1]);
        for (int i = start + 2;i <= end;i++) {
            int temp = second;
            second = Math.max(second, first + nums[i]);
            first = temp;
        }
        return second;
    }
}
